package leetcode.code0589;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Solution {

	class Node {
		public int val;
		public List<Node> children;

		public Node() {
		}

		public Node(int _val) {
			val = _val;
		}

		public Node(int _val, List<Node> _children) {
			val = _val;
			children = _children;
		}
	}

	public List<Integer> preorder(Node root) {
		List<Integer> ans = new ArrayList<>();
		if (root == null) {
			return ans;
		}
		Stack<Node> stack = new Stack<>();
		stack.add(root);
		while (!stack.isEmpty()) {
			Node cur = stack.pop();
			ans.add(cur.val);
			List<Node> child = cur.children;
			int size = child.size();
			for (int i = size - 1; i >= 0; i--) {
				stack.add(child.get(i));
			}
		}
		return ans;
	}
}
